﻿using System;
using System.Collections.Generic;
using System.Linq;
using SQLStore;
using System.Data.Entity.Migrations;
using System.Linq.Expressions;
using Models;

namespace Services
{
    public class DictionaryServices : IDataAccess<Dictionary>
    {
        CMSDBContext db ;
        public DictionaryServices()
        {
            db = new CMSDBContext();
        }
        public DictionaryServices(string connectionString)
        {
            db = new CMSDBContext(connectionString);
        }

        public int Delete(object objValue)
        {
            int Id = (int)objValue;
            Dictionary entity = db.Set<Dictionary>().Where(t => t.Id == Id).FirstOrDefault();
            if (entity != null)
                db.Set<Dictionary>().Remove(entity);

            throw new NotImplementedException();
        }

        public bool Exists(object objValue)
        {
            return db.Set<Dictionary>().Any(t => t.Name == objValue.ToString());
        }

        public bool Exists(Expression<Func<Dictionary, bool>> exp)
        {
            return db.Set<Dictionary>().Any(exp);
        }

        public Dictionary GetByID(object objValue)
        {
            int id = (int)objValue;
            return db.Set<Dictionary>().Where(t => t.Id == id).FirstOrDefault();
        }

        public List<Dictionary> GetList(Dictionary entity)
        {
            throw new NotImplementedException();
        }
        public List<Dictionary> GetList(Expression<Func<Dictionary, bool>> exp)
        {
            return db.Set<Dictionary>().Where(exp).ToList();
        }

        public int Save(Dictionary entity)
        {
            db.Set<Dictionary>().AddOrUpdate(entity);
            return Sumbit();
        }

        int Sumbit()
        {
            return db.SaveChanges();
        }
    }
}